from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from module_admin.entity.vo.cms_device_vo import DeviceModel, DevicePageObject, DeviceForQuery
from module_admin.service.cms_device_service import CmsDeviceService
from module_admin.dao.dept_dao import *
from module_admin.service.login_service import get_current_user_details
from utils.page_util import get_page_obj
from utils.response_util import *
from utils.log_util import *
from utils.time_format_util import object_format_datetime

deviceController = APIRouter(dependencies=[Depends(get_current_user_details)])


@deviceController.post("/record/create", response_model=DeviceModel, summary="设备档案-新增")
async def create_device(request: Request, query: DeviceModel, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDeviceService.create_device(query_db, query)
        return response_200(data=dept_query_result, message="新增成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@deviceController.get("/record/delete/{id}", summary="设备档案-删除")
async def delete_device(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDeviceService.delete_device(query_db, id)
        return response_200(data=dept_query_result, message="删除成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@deviceController.post("/record/update", summary="设备档案-编辑")
async def update_device(request: Request, device_update: DeviceModel, db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDeviceService.update_device(db, device_update)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@deviceController.get("/record/get/{id}", summary="设备档案-详情")
async def get_device(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDeviceService.get_device(query_db, id)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@deviceController.post("/record/page", summary="设备档案-分页")
async def page_device(request: Request, device_page_query: DevicePageObject, db: Session = Depends(get_db)):
    try:
        query_object = DeviceForQuery(**device_page_query.dict())
        page_result = CmsDeviceService.list(db, query_object)
        list_format_datetime(page_result)
        # 分页操作
        page_result_after = get_page_obj(page_result, device_page_query.page_num, device_page_query.page_size)
        # 处理
        return response_200(data=page_result_after, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


